import redis
from flask import Flask,session
from flask_session import Session
from flask_sqlalchemy import SQLAlchemy
from flask_wtf.csrf import CSRFProtect


class Config(object):
    """配置信息"""

    # 配置sqlalchemy连接
    SQLALCHEMY_DATABASE_URI = 'mysql://root:mysql@127.0.0.1/information'
    SQLALCHEMY_TRACK_MODIFICATIONS = False

    # 配置redis
    REDIS_HOST = "127.0.0.1"
    REDIS_PORT = 3306

    # 配置秘钥
    SECRET_KEY = "BI4hiNozWFv7mlT+uATQ3E0gay7EQ+7NM52+8iCzIdI="

    # 配置flask_Session扩展
    SESSION_TYPE = 'redis'  # 设置要同步的位置
    SESSION_REDIS = redis.StrictRedis(host=REDIS_HOST, port=REDIS_PORT)
    SESSION_USE_SIGNER = True  # 开启签名, 保证数据安全
    PERMANENT_SESSION_LIFETIME = 86400 * 7  # 配置过期时间


# 程序实例化
app = Flask(__name__)

# 引入配置信息
app.config.from_object(Config)

# 创建sqlalchemy对象，与程序实例关联
db = SQLAlchemy(app)

# 创建redis对象
redis_store = redis.StrictRedis(host=Config.REDIS_HOST, port=Config.REDIS_PORT)

# 开启csrf保护
CSRFProtect(app)

# 将Session数据同步到redis中
Session(app)


@app.route('/')
def index():
    session['name'] = 'itheima'
    return 'HelloWorld'


if __name__ == '__main__':
    app.run(debug=True)
